/*
 * @(#)IndexController.java 1.0 2019年1月8日下午4:10:11
 *
 * 
 * Copyright (c) , Inc. All rights reserved.
 */
package com.saturn.config.server.web;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.saturn.config.server.base.dto.JsonCode;
import com.saturn.config.server.base.dto.JsonResult;
import com.saturn.config.server.base.dto.TreeNode;
import com.saturn.config.server.base.entity.AppInfo;
import com.saturn.config.server.base.entity.SysUser;
import com.saturn.config.server.base.enums.EnvTypeEnum;
import com.saturn.config.server.service.AppInfoService;

/**
 * <dl>
 * <dt><b>Title:</b></dt>
 * <dd>none</dd>
 * <dt><b>Description:</b></dt>
 * <dd>
 * <p>
 * none</dd>
 * </dl>
 *
 * @author tanks
 * @version 1.0, 2019年1月8日
 * @since JdbcConfigServerAdmin
 * 
 */
@Controller
public class IndexController {

	@Autowired 
	private AppInfoService appInfoService;
	@Value("${app.sign.privite.key:6e776f703868396a7370773131307077}")
	private String priKey;
	
	@RequestMapping(value = "/")
	public String index() {
		return "index";
	}

	@RequestMapping(value = "/configLogsManage")
	public String configLogsManage() {
		return "config_logs_manage";
	}
	
	@RequestMapping(value = "/appConfigManage")
	public String appConfigManage() {
		return "app_config_manage";
	}
	
	@RequestMapping(value = "/appInfoManage")
	public String appInfoManage() {
		return "app_info_manage";
	}
	
	@RequestMapping(value = "/appVersionManage")
	public String appVersionManage() {
		return "app_version_manage";
	}
	
	@RequestMapping(value = "/sysUserManage")
	public String sysUserManage() {
		return "sys_user_manage";
	}
	
	@RequestMapping(value = "getMainPageMenu",produces="application/json")
	public @ResponseBody List<TreeNode> getMainPageMenu(HttpServletRequest request) {
		SysUser loginUser = (SysUser)request.getSession().getAttribute("useInfo");
		List<TreeNode> treeNodes = new ArrayList<TreeNode>();
		TreeNode node1 = new TreeNode();
		node1.setId("111111");
		node1.setText("应用列表管理");
		node1.setResourceType(new BigDecimal("2"));
		node1.setLink("/appInfoManage");
		node1.setLeaf(true);
		treeNodes.add(node1);
		TreeNode node3 = new TreeNode();
		node3.setId("333333");
		node3.setText("配置列表管理");
		node3.setLink("/appConfigManage");
		node3.setResourceType(new BigDecimal("2"));
		node3.setLeaf(true);
		treeNodes.add(node3);
		TreeNode node4 = new TreeNode(); node4.setId("444444");
		node4.setText("历史版本管理"); node4.setLink("/appVersionManage");
		node4.setResourceType(new BigDecimal("2")); node4.setLeaf(true);
		treeNodes.add(node4);
		if( "A".equals(loginUser.getType())) {
			TreeNode node5 = new TreeNode(); node5.setId("55555");
			node5.setText("系统用户管理"); node5.setLink("/sysUserManage");
			node5.setResourceType(new BigDecimal("2")); 
			node5.setLeaf(true);
			treeNodes.add(node5);
		}
		return treeNodes;
	}
	
	@CrossOrigin
	@RequestMapping(value = "/getAllMenu", produces = "application/json")
	public JsonResult getAllMenu() {
		JsonResult jsonResult = new JsonResult();
		List<AppInfo> projectInfos = appInfoService.list();
		jsonResult.setRespResult(Boolean.TRUE.booleanValue());
		jsonResult.setRespResult(projectInfos);
		return jsonResult;
	}

	/**
	 * 获取所有项目用于下拉选择
	 * @return
	 */
	@RequestMapping(value = "getAllEnviroments",produces="application/json")
	public  @ResponseBody Map<String,List<JsonCode>> getAllEnviroments(String excludeAll) {
		List<JsonCode> jsonCodes = new ArrayList<JsonCode>();
		Map<String,List<JsonCode>> combox = new HashMap<String,List<JsonCode>>();
		if( StringUtils.isBlank(excludeAll) ) {
			jsonCodes.add(new JsonCode("","全部"));
		}
		for (EnvTypeEnum entity : EnvTypeEnum.values()) {
			jsonCodes.add(new JsonCode(entity.getCode(),entity.getDesc()));
		}
		combox.put("comboBox", jsonCodes);
		return combox;
	}

	/**
	 * 获取所有项目用于下拉选择
	 * @return
	 */
	@RequestMapping(value = "getApplications",produces="application/json")
	public  @ResponseBody Map<String,List<JsonCode>> getApplications(String excludeAll,String envType) {
		List<JsonCode> jsonCodes = new ArrayList<JsonCode>();
		QueryWrapper<AppInfo> queryWrapper = Wrappers.query();
		if( StringUtils.isNotBlank(envType) ) {
			queryWrapper.eq("env_type", envType);
		}
		List<AppInfo> projectInfos = appInfoService.list(queryWrapper);
		Map<String,List<JsonCode>> combox = new HashMap<String,List<JsonCode>>();
		if( StringUtils.isBlank(excludeAll) ) {
			jsonCodes.add(new JsonCode("","全部"));
		}
		if( projectInfos!=null && projectInfos.size()>0 ) {
			for( AppInfo proj:projectInfos ) {
				if( StringUtils.isNotBlank(envType) && !proj.getEnvType().equals(envType) ) {
					continue;
				}
				JsonCode jsonCode = new JsonCode(String.valueOf(proj.getAppId()),proj.getAppName() + "[" + EnvTypeEnum.transfer(proj.getEnvType()).getDesc() + "-" + proj.getDescription() + "]");
				jsonCodes.add(jsonCode);
			}
		}
		combox.put("comboBox", jsonCodes);
		return combox;
	}
	
}
